package com.hzb.gulimall.ware.service.impl;

import com.hzb.common.utils.R;
import com.hzb.gulimall.ware.feign.ProductFeignService;
import com.hzb.gulimall.ware.vo.WareSkuStockVo;
import com.sun.xml.internal.bind.v2.TODO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzb.common.utils.PageUtils;
import com.hzb.common.utils.Query;

import com.hzb.gulimall.ware.dao.WareSkuDao;
import com.hzb.gulimall.ware.entity.WareSkuEntity;
import com.hzb.gulimall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private  WareSkuDao wareSkuDao;
    @Autowired
    private ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        /**
         * skuId:
         * wareId
         */
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", skuId)
                .eq("ware_id", wareId));
        if (list == null || list.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //当远程调用异常的时候使用try catch可以不用回滚整个事务
            //TODO 还可以使用什么办法在异常出现以后不回滚事务呢？
            try {
                R info = productFeignService.info(skuId);
                if (info.getCode() == 0){
                    Map<String,Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                    wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
                }
            }catch (Exception e){

            }

            this.save(wareSkuEntity);
        }else {
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }

    }

    @Override
    public List<WareSkuStockVo> getSkuHasstock(List<Long> skuIds) {

        List<WareSkuStockVo> collect = skuIds.stream().map(skuId -> {
            WareSkuStockVo wareSkuStockVo = new WareSkuStockVo();
            Long count = baseMapper.getWareStock(skuId);
            wareSkuStockVo.setSkuId(skuId);
            wareSkuStockVo.setHasStock(count==null?false:count>0);
            return wareSkuStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}